import asyncio
import json
import time

from curl_cffi import requests
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs
import hashlib

with open('h5st514.js', 'r', encoding='utf-8') as f:
    js = f.read()

js_com = execjs.compile(js)


def sha_256(message):
    return hashlib.sha256(message.encode('utf-8')).hexdigest()


def convert_cookie_dict_to_string(cookie_dict):
    # Join each key-value pair with '=' and then join all pairs with '; '
    cookie_string = '; '.join([f"{key}={value}" for key, value in cookie_dict.items()])
    return cookie_string


url = "https://api.m.jd.com/"


def generate_h5st(params):
    # 解析原始 body
    body_str = params['body']
    body = json.loads(body_str) if isinstance(body_str, str) else body_str

    # 创建用于 SHA256 加密的 boddy
    boddy = {
        "keyword": body['keyword'],
        "qrst": body['qrst'],
        "stock": body['stock'],
        "pvid": body['pvid'],
        "isList": body['isList'],
        "page": body['page'],
        "s": body['s'],
        "click": body['click'],
        "log_id": body['log_id'],
        "show_items": body['show_items'],
    }

    # 创建用于生成 h5st 的 a 对象
    a = {
        "appid": params.get('appid', 'search-pc-java'),
        "body": sha_256(json.dumps(boddy, separators=(',', ':'))),
        "clientVersion": params.get('clientVersion', '1.0.0'),
        "client": params.get('client', 'pc'),
        "functionId": params.get('functionId', 'pc_search_s_new'),
        "t": int(time.time() * 1000),  # 使用当前时间戳
    }

    # 调用 JS 的 PSign.sign 方法生成 h5st
    h5st = js_com.call('getH5st', a)

    return h5st


headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "origin": "https://search.jd.com",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://search.jd.com/",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
    "x-referer-page": "https://search.jd.com/Search",
    "x-rp-client": "h5_1.0.0"
}
cookies = {
    "b_webp": "1",
    "b_avif": "1",
    "jcap_dvzw_fp": "_HIVLiZCIEZDIcHwXjPU43al5uvsYIwPfs4O6VE9nqABptbi3ASG0seWjzU_o2F4bpTtKrH1VphdN9RwziykSA==",
    "whwswswws": "",
    "__jdv": "122270672%7Cdirect%7C-%7Cnone%7C-%7C1751545604909",
    "__jdu": "1751545604909534518369",
    "pinId": "briEibRb-h1YgxkG81uaz7V9-x-f3wj7",
    "pin": "jd_7eac037a858fc",
    "unick": "jd_8js7q5tn2s7n2h",
    "_tp": "aO5XtogPiplRTZPyN6P5Vkd5JC%2FQJe7kEmh0g0oTaNI%3D",
    "_pst": "jd_7eac037a858fc",
    "ipLoc-djd": "19-1607-0-0",
    "shshshfpa": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "shshshfpx": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "areaId": "19",
    "mail_times": "2%2C1",
    "PCSYCityID": "CN_440000_440300_0",
    "autoOpenApp_downCloseDate__jd_czlogin_openapp_": "1752224419174_1",
    "b_dw": "1912",
    "b_dh": "954",
    "b_dpr": "1",
    "autoOpenApp_downCloseDate_jd_homePage": "1752226834147_1",
    "autoOpenApp_downCloseDate_auto": "1752226845692_1800000",
    "commonAddress": "0",
    "regionAddress": "1%2C72%2C55674%2C0",
    "mt_xid": "V2_52007VwMUVl1cV14YSBxUBG8HEFdZWFtYGEEpXVBjCkcCCAhOWBsdGUAAZQAUTg1eVFMDHhoLDDdQEQBeXFBdL0oYXwF7AhROXVhDWhlCHFQOZwMiUG1YYlMfSB1eBmMEFFJcX1ZZGk4RWQ1XAxFRXw%3D%3D",
    "warehistory": '"100124552423,10092745719292,100124552423,100126228944,"',
    "autoOpenApp_downCloseDate_autoOpenApp_autoPromptly": "1752227819923_1",
    "pt_st": "1_b4icbLDrQArn0cxP2HrWd52OePOExea0Jf3d5GU7ZDNxycxozqafTLZP8UFvD2yptQsomwf2-UYVZyrTC5wQqynLrLGiyG3q8nI6i5hSWFl4AAeNWC95FX4i0R71LSWFqJ2DvHQ2cldUl8WEZDlVz6MZZ3lMJJeVb4ISQL_qfGpqpLc1BYbrikhDMRA9vTqn61nU9HDMgoBQNAqt7W5KfiNomTyxnBlLMxruopZY",
    "3AB9D23F7A4B3CSS": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX7D3EMJYAAAAACK4KEKL4WK6B6AX",
    "_gia_d": "1",
    "mba_muid": "1751545604909534518369",
    "mba_sid": "1752228585673337709007.1",
    "wlfstk_smdl": "vb8h6rndq83lwdqsmz4gfxudjzftu1kp",
    "TrackID": "1niKUQYb-rtx_W8NqIr7p794udrddHlWmBsM7VCqfFSRqsnFBOKtSNGv5MwSaaIptwAekpQGc-lVbatyIbIIsdh6kB0gSXD0Vf_HOAU3UPpgyNscP-uKG7aFXOVOpu3Hz",
    "thor": "2B3FAB022DDA7FFF772855629D82E8BA86B6B654FC776E1930AA7FEB1EB3100E4352BA9EF36DB2CA5363601A26FB8D56926934F02FEC214A855D33E9523DF9BBF6219112C431FBD129D5E5F946C5285F4F5B6BCB9C68A64458D0B167F141477BCC221C42AC5B82E714C37179E720FA1CA416456728D98223977A00EC6720051F59117BA692A7FA2CB8A8C792545F0FC0C1CB9B05CAF59D31B40631A5DF1C0593",
    "light_key": "AASBKE7rOxgWQziEhC_QY6yaY-qjKdTSlPyvgw1EnJwRdrY19x9HuXOKE36tg6wrJUawo6E8",
    "ceshi3.com": "000",
    "__jda": "143920055.1751545604909534518369.1751545604.1752224288.1752226846.4",
    "__jdc": "143920055",
    "jsavif": "1",
    "flash": "3_D_IhI3sksOIWvtD2rCYrp0CIivlUi2GAApk9_VhmjLmWtkENiHrHbISHdscrVY0x_x5yQL1Cpj4bysCm65tV_W-qKEV4IbBl9MvLc6syB93OzQUWSf0DmdgZ1NK9nfYWkuJfErETMjHTv-2E9vr5Z4F4QDLRDH5XmHx_YEvYrN20tbJZP-c3Oe**",
    "3AB9D23F7A4B3C9B": "GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBE",
    "__jdb": "143920055.39.1751545604909534518369|4.1752226846",
    "sdtoken": "AAbEsBpEIOVjqTAKCQtvQu17b_4CPLpklkxE2OT44jOsk96VMMz5lyS8fEPdU0wYr6yoqZ4N_NNfh8jBkkm6jmffcqEsz8QyI0g8wfvIgBevAW8DBqI4fujImQeAz3qZSsEoPJJW",
    "shshshfpb": "BApXSAJH_-_JAYEqgcJFHsyDPBX7ULrZhBhMVUQdq9xJ1MppMe462",
}
url = "https://api.m.jd.com/"
params = {
    "appid": "search-pc-java",
    "functionId": "pc_search_s_new",
    "client": "pc",
    "clientVersion": "1.0.0",
    "t": "1751559477171",
    "body": "{\"keyword\":\"啤酒\",\"qrst\":\"1\",\"stock\":\"1\",\"pvid\":\"21dd9be9ae5e46b5bbab6e9deefe93f8\",\"isList\":0,\"page\":\"3\",\"s\":\"55\",\"click\":\"0\",\"log_id\":\"1751559471161.2229\",\"show_items\":\"\"}",
    "loginType": "3",
    "uuid": "143920055.17465391768081442966342.1746539176.1749480008.1751558319.6",
    "area": "19_1607_0_0",
    "h5st": "20250704001758191;gxzd3xgxiqah2hh4;f06cc;tk03wae2f1bb418nW1dtxzc4U9HTumFrPDjqyj9tc_EnjWqB2ml1W8K4QF7oDoW3Os88WYF922DCYE5s05nXSINkZxtr;33b6dcd73d961e0104b672619c786fc6;5.1;1751559477191;ri_uxFOm6OoU8OYW9JoQHN3iKFnQHlsm0msSIlsmOGuj4qrm0mMTLhImOuMsCmsiJp7hLVbV_a7W7ebiLNrgIt7i8KrhItrV7Srg3eIVLlsm0msSo94VMZ4RMusmk_MmNpIV8e4hIt7W4qYV_mYW1eYWMN7h7aIV4qbh4OriIVLmOGLm7pIRAp4WMusmk_ciBuMgMebRMlsmOGujMSLj92ch4xZVCJIVPZrUMuMgMWHmOuMsCmsU3iqi4Foi6a7iXlLdMuMgM64TK1YW8lsmOGujMm7iAJ4ZMuMgMWoSMusmk_cPOuMs8uMgMqbi5lImOusmOGuj8qrm0msi9aHWMusmOuMsCObjOGLm8qbRMlsmOusmk_Mm-5sUpxod-dIi-lsm0mcT-dITNlHmOusmOGuj_uMgMObRMlsmOusmk_siOGLm3aHWMusmOuMsCS7iOGLm4aHWMusmOuMsCurm0mch5lImOusmOGuj_uMgMebRMlsmOusmk_sh7uMgMibRMlsmOusmk_Mm42ciAuLmOGLm9aHWMusmOuMsCurm0m8U3lsmOusmk_chOGLm79ImOusmOGuj_uMgM_ImOusmOGuj_uMgMe4RMusmOuMsztMgMeITJdnQJlsmOGujxtsmkm8WINbh2q4h7u7WNZ4hPdIUMuMgMmrSMusmOuMsztMgMunSMusmk_Mm6WrQOCrh42YUXt8g_2si9usZgt8S3xoVAJ4ZMuMgMqYR7lsmOG_Q;b43030a94ed431da3d615a5da512d76a;tenjKJKT-JoRL1YRI9MT-J4S8ZIZ61YVF94WCeHTJJoTL9cQKxIWCeYU_tXW",
    "x-api-eid-token": "jdd03QZ2GMYPVPE236JW447KCKH3YVMUW5QY7QT67A6K7X5QCOJMTKXDTAFGEKFLXCTDZBREIFPGYWNY7IDCGXXTKK22MDUAAAAMX2EKGZEYAAAAACVPM7GMSRFBU4UX"
}
h5st = generate_h5st(params)
print(h5st)
print(len(h5st))
params['h5st'] = h5st
response = requests.get(url, headers=headers, cookies=cookies, params=params)

print(response.text)
print(response)
